package com.example.pulseanalyse.veinType;

import android.text.TextUtils;
import android.util.Log;

import com.example.pulseanalyse.veinPoint.myClassPluseAnalysisSD;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: yyw
 * time: 2020/12/9 10:01
 * desc: MainWindow.xaml 的交互逻辑
 */
public class MainWindow {
    private static final String TAG = "MainWindow";

    /// 脉型分析类
    newPulseAnalysis _newAnalysis = new newPulseAnalysis();

    /// 判断综合脉型类
    synContext SynContext = new synContext();

    public String PulseTypeFu = "";
    public String PulseTypeZhong = "";
    public String PulseTypeChen = "";

    /// 开始调用
    public void getPluseResult(myClassPluseAnalysisSD analysisSDFu, myClassPluseAnalysisSD analysisSDZhong, myClassPluseAnalysisSD analysisSDChen) {

        //单峰、双峰、多峰
        int _crestCountFu = 1;
        int _crestCountZhong = 1;
        int _crestCountChen = 1;
        if ((analysisSDFu.单峰List.size() >= analysisSDFu.双峰List.size()) && (analysisSDFu.单峰List.size() >= analysisSDFu.多峰List.size())) {
            _crestCountFu = 1;
        } else if ((analysisSDFu.双峰List.size() >= analysisSDFu.单峰List.size()) && (analysisSDFu.双峰List.size() >= analysisSDFu.多峰List.size())) {
            _crestCountFu = 2;
        } else if ((analysisSDFu.多峰List.size() >= analysisSDFu.单峰List.size()) && (analysisSDFu.多峰List.size() >= analysisSDFu.双峰List.size())) {
            _crestCountFu = 3;
        }
        if ((analysisSDZhong.单峰List.size() >= analysisSDZhong.双峰List.size()) && (analysisSDZhong.单峰List.size() >= analysisSDZhong.多峰List.size())) {
            _crestCountZhong = 1;
        } else if ((analysisSDZhong.双峰List.size() >= analysisSDZhong.单峰List.size()) && (analysisSDZhong.双峰List.size() >= analysisSDZhong.多峰List.size())) {
            _crestCountZhong = 2;
        } else if ((analysisSDZhong.多峰List.size() >= analysisSDZhong.单峰List.size()) && (analysisSDZhong.多峰List.size() >= analysisSDZhong.双峰List.size())) {
            _crestCountZhong = 3;
        }
        if ((analysisSDChen.单峰List.size() >= analysisSDChen.双峰List.size()) && (analysisSDChen.单峰List.size() >= analysisSDChen.多峰List.size())) {
            _crestCountChen = 1;
        } else if ((analysisSDChen.双峰List.size() >= analysisSDChen.单峰List.size()) && (analysisSDChen.双峰List.size() >= analysisSDChen.多峰List.size())) {
            _crestCountChen = 2;
        } else if ((analysisSDChen.多峰List.size() >= analysisSDChen.单峰List.size()) && (analysisSDChen.多峰List.size() >= analysisSDChen.双峰List.size())) {
            _crestCountChen = 3;
        }

        if (analysisSDFu.p主波幅高 > 0) {
            PulseTypeFu = getPulseType(
                    analysisSDFu.p心率,//脉率
                    analysisSDFu.p是否间歇,//是否有间歇
                    analysisSDFu.p间歇是否规律,//间歇是否有规律
                    _crestCountFu,//波峰数量
                    analysisSDFu.p主波幅高,//测量的波幅高
                    analysisSDFu.p主波上部周期,//p主波上部周期
                    analysisSDFu.p周期,//周期
                    analysisSDFu.p潮波幅高,//潮波高
                    analysisSDFu.降中峡h4,//降中峡高
                    analysisSDFu.p主波入射角,//入射角
                    analysisSDFu.p主波夹角,//主波角
                    analysisSDFu.p主波上升期,//主波上升期
                    analysisSDFu.p主波幅高,//浮取h1
                    analysisSDZhong.p主波幅高,//中取h1
                    analysisSDChen.p主波幅高,//沉取h1
                    analysisSDFu.p主波夹角,//浮取的主波夹角
                    analysisSDZhong.p主波夹角,//中取的主波夹角
                    "浮取",
                    analysisSDFu.p主波最大间隔//周期的最大间隔
            );
        }

        if (analysisSDZhong.p主波幅高 > 0) {
            PulseTypeZhong = getPulseType(
                    analysisSDZhong.p心率,//脉率
                    analysisSDZhong.p是否间歇,//是否有间歇
                    analysisSDZhong.p间歇是否规律,//间歇是否有规律
                    _crestCountZhong,//波峰数量
                    analysisSDZhong.p主波幅高,//测量的波幅高
                    analysisSDZhong.p主波上部周期,//p主波上部周期
                    analysisSDZhong.p周期,//周期
                    analysisSDZhong.p潮波幅高,//潮波高
                    analysisSDZhong.降中峡h4,//降中峡高
                    analysisSDZhong.p主波入射角,//入射角
                    analysisSDZhong.p主波夹角,//主波角
                    analysisSDZhong.p主波上升期,//主波上升期
                    analysisSDFu.p主波幅高,//浮取h1
                    analysisSDZhong.p主波幅高,//中取h1
                    analysisSDChen.p主波幅高,//沉取h1
                    analysisSDFu.p主波夹角,//浮取的主波夹角
                    analysisSDZhong.p主波夹角,//中取的主波夹角
                    "中取",
                    analysisSDZhong.p主波最大间隔//周期的最大间隔
            );
        }
        if (analysisSDChen.p主波幅高 > 0) {
            PulseTypeChen = getPulseType(
                    analysisSDChen.p心率,//脉率
                    analysisSDChen.p是否间歇,//是否有间歇
                    analysisSDChen.p间歇是否规律,//间歇是否有规律
                    _crestCountChen,//波峰数量
                    analysisSDChen.p主波幅高,//测量的波幅高
                    analysisSDChen.p主波上部周期,//p主波上部周期
                    analysisSDChen.p周期,//周期
                    analysisSDChen.p潮波幅高,//潮波高
                    analysisSDChen.降中峡h4,//降中峡高
                    analysisSDChen.p主波入射角,//入射角
                    analysisSDChen.p主波夹角,//主波角
                    analysisSDChen.p主波上升期,//主波上升期
                    analysisSDFu.p主波幅高,//浮取h1
                    analysisSDZhong.p主波幅高,//中取h1
                    analysisSDChen.p主波幅高,//沉取h1
                    analysisSDFu.p主波夹角,//浮取的主波夹角
                    analysisSDZhong.p主波夹角,//中取的主波夹角
                    "中取",
                    analysisSDChen.p主波最大间隔//周期的最大间隔
            );
        }

        getAveData(analysisSDFu, analysisSDZhong, analysisSDChen);

    }


    /// 平均脉率
    public int AvePulseRate = 80;
    /// 平均脉型
    public String AvePulseType = "";


    /// <summary>
    /// 综合脉型
    /// </summary>
    private void getAveData(myClassPluseAnalysisSD analysisSDFu, myClassPluseAnalysisSD analysisSDZhong, myClassPluseAnalysisSD analysisSDChen) {
        int count = 0;
        if (analysisSDFu.p心率 != 0) {
            count++;
        }
        if (analysisSDZhong.p心率 != 0) {
            count++;
        }
        if (analysisSDChen.p心率 != 0) {
            count++;
        }
        if (count != 0) {
            AvePulseRate = (new Double(analysisSDFu.p心率).intValue() + new Double(analysisSDZhong.p心率).intValue() + new Double(analysisSDChen.p心率).intValue()) / count;
        }

        AvePulseType = "";

        ///先将三个脉型相加,为了判断弦、涩、滑、促、结、代
        String pulseTypeFu = PulseTypeFu, pulseTypeZhong = PulseTypeZhong, pulseTypeChen = PulseTypeChen;

        if (pulseTypeFu == "") {
            pulseTypeFu = "无";
        }
        if (pulseTypeZhong == "") {
            pulseTypeZhong = "无";
        }
        if (pulseTypeChen == "") {
            pulseTypeChen = "无";
        }

        String AvePulseType1 = pulseTypeFu + "," + pulseTypeZhong + "," + pulseTypeChen;

        ///浮 + 中 + 沉 三种取脉力度脉型累加
        List<String> _typeList = new ArrayList<>();

        String[] strSplit = AvePulseType1.split(",");
        for (int i = 0; i < strSplit.length; i++) {
            if (strSplit[i] != "") {
                _typeList.add(strSplit[i]);
            }
        }

        //找到最佳脉图的脉型
        String avePulseType = "";

        if (analysisSDFu.p主波幅高 > analysisSDZhong.p主波幅高 &&
                analysisSDFu.p主波幅高 > analysisSDChen.p主波幅高) {
            avePulseType = pulseTypeFu;
        } else if (analysisSDZhong.p主波幅高 > analysisSDFu.p主波幅高 &&
                analysisSDZhong.p主波幅高 > analysisSDChen.p主波幅高) {
            avePulseType = pulseTypeZhong;
        } else {
            avePulseType = pulseTypeChen;
        }
//        if (Double.parseDouble(dataModel.PulseZhuBoFuGaoFu) > Double.parseDouble(dataModel.PulseZhuBoFuGaoZhong) &&
//                Double.parseDouble(dataModel.PulseZhuBoFuGaoFu) > Double.parseDouble(dataModel.PulseZhuBoFuGaoChen)) {
//            avePulseType = dataModel.PulseTypeFu;
//        } else if (Double.parseDouble(dataModel.PulseZhuBoFuGaoZhong) > Double.parseDouble(dataModel.PulseZhuBoFuGaoFu) &&
//                Double.parseDouble(dataModel.PulseZhuBoFuGaoZhong) > Double.parseDouble(dataModel.PulseZhuBoFuGaoChen)) {
//            avePulseType = dataModel.PulseTypeZhong;
//        } else {
//            avePulseType = dataModel.PulseTypeChen;
//        }

        synContext1 context1 = new synContext1();
        ///返回综合脉型
        try {
            AvePulseType = context1.setValue(_typeList, avePulseType);
            Log.d(TAG, "综合脉型===" + AvePulseType);
        } catch (Exception e) {
            Log.d(TAG, "综合脉型出错=====" + e.getMessage());
        }

    }


    /// <summary>
    /// 上下文 策略+简单工厂类
    /// </summary>
    mutexContext mutex = null;

    /// <summary>
    /// 得到脉型
    /// </summary>
    /// <param name="_pulseRate">脉率</param>
    /// <param name="_isInterval">是否有间歇</param>
    /// <param name="isRule">间歇是否有规律</param>
    /// <param name="_crestCount">波峰数量</param>
    /// <param name="_h1">测量的波幅高</param>
    /// <param name="_w">p主波上部周期</param>
    /// <param name="_t">周期</param>
    /// <param name="_h3">潮波高</param>
    /// <param name="_h4">降中峡高</param>
    /// <param name="_incidenceAngle">入射角</param>
    /// <param name="_mainWaveAngle">主波角</param>
    /// <param name="_t1">主波上升期</param>
    /// <param name="_h1Fu">浮取h1</param>
    /// <param name="_h1Zhong">中取h1</param>
    /// <param name="_h1Chen">沉取h1</param>
    /// <param name="_zhuBoJiaJiaoFu">浮取的主波夹角</param>
    /// <param name="_zhuBoJiaJiaoZhong">中取的主波夹角</param>
    /// <param name="_maxJianGe">周期的最大间隔</param>
    private String getPulseType(double _pulseRate, boolean _isInterval, boolean isRule, int _crestCount, double _h1, double _w,
                                double _t, double _h3, double _h4, double _incidenceAngle, double _mainWaveAngle, double _t1,
                                double _h1Fu, double _h1Zhong, double _h1Chen, double _zhuBoJiaJiaoFu, double _zhuBoJiaJiaoZhong, String _plusePresurre, double _maxJianGe) {
        String _pulseType = "";

        List<String> _pulseTypeList = new ArrayList<>();
        List<String> _pulseTypeListTrue = new ArrayList<>();
        List<String> _pulseTypeListTrue1 = new ArrayList<>();


        List<String> _strPeriodicIntervalsList = _newAnalysis.ReturnMaxPeriodicIntervals(_maxJianGe); //新添
        List<String> _strRateList = _newAnalysis.ReturnPulseRate(_pulseRate);
        List<String> _strForceUniformityList = _newAnalysis.ReturnPulseForceUniformity();
        List<String> _strPulseLVList = _newAnalysis.ReturnPulseLV(_isInterval, isRule);
        List<String> _strCrestList = _newAnalysis.ReturneCrestCount(_crestCount);
        List<String> _strForceList = _newAnalysis.ReturnPulseForce(_h1);
        List<String> _strTensionList = _newAnalysis.ReturnPulseTension(_w, _t, _h1, _h3, _h4);
        List<String> _strFluencyList = _newAnalysis.ReturnFluency(_incidenceAngle, _mainWaveAngle, _t1);
        List<String> _strPositionList = _newAnalysis.ReturnPulsePosition(_h1Fu, _h1Zhong, _h1Chen);
        List<String> _strDeformabList = _newAnalysis.ReturnDeformability(_zhuBoJiaJiaoFu, _zhuBoJiaJiaoZhong, _h1Fu, _h1Zhong, _h1Chen, _h4, _h1);

        _pulseTypeList.addAll(_strRateList);
        _pulseTypeList.addAll(_strForceUniformityList);
        _pulseTypeList.addAll(_strPulseLVList);
        _pulseTypeList.addAll(_strCrestList);
        _pulseTypeList.addAll(_strForceList);
        _pulseTypeList.addAll(_strTensionList);
        _pulseTypeList.addAll(_strFluencyList);
        _pulseTypeList.addAll(_strPositionList);
        _pulseTypeList.addAll(_strDeformabList);

        Map<String, List<String>> listMap1 = GroupingUtil.dividerStrList2(_pulseTypeList);
        for (String key1 : listMap1.keySet()) {
            // Console.WriteLine(item.Key + "重现了:" + item.Count() + "次");
            for (int i = 0; i < _newAnalysis.conditionList.size(); i++) {
                if (_newAnalysis.conditionList.get(i).PulseType.equals(key1) && _newAnalysis.conditionList.get(i).ConditionCount == listMap1.get(key1).size()) {
                    _pulseTypeListTrue.add(_newAnalysis.conditionList.get(i).PulseType);
                    break;
                }
            }
        }
//        foreach(var item in _pulseTypeList.GroupBy(c = > c))
//        {
//            // Console.WriteLine(item.Key + "重现了:" + item.Count() + "次");
//            for (int i = 0; i < _newAnalysis.conditionList.Count; i++) {
//                if (_newAnalysis.conditionList[i].PulseType == item.Key && _newAnalysis.conditionList[i].ConditionCount == item.Count()) {
//                    _pulseTypeListTrue.Add(_newAnalysis.conditionList[i].PulseType);
//                    break;
//                }
//            }
//        }
        if (_pulseTypeListTrue.size() == 0) {
            _pulseType = "平";
        } else {
            mutex = new mutexContext("浮");
            String _strFu = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strFu) && _pulseTypeListTrue.contains("浮")) {
                _pulseTypeListTrue.remove("浮");
            }

            mutex = new mutexContext("沉");
            String _strChen = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strChen) && _pulseTypeListTrue.contains("沉")) {
                _pulseTypeListTrue.remove("沉");
            }

            mutex = new mutexContext("迟");
            String _strChi = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strChi) && _pulseTypeListTrue.contains("迟")) {
                _pulseTypeListTrue.remove("迟");
            }

            mutex = new mutexContext("数");
            String _strShu = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strShu) && _pulseTypeListTrue.contains("数")) {
                _pulseTypeListTrue.remove("数");
            }

            mutex = new mutexContext("滑");
            String _strHua = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strHua) && _pulseTypeListTrue.contains("滑")) {
                _pulseTypeListTrue.remove("滑");
            }

            mutex = new mutexContext("弦");
            String _strXian = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strXian) && _pulseTypeListTrue.contains("弦")) {
                _pulseTypeListTrue.remove("弦");
            }

            //mutex = new mutexContext("实");
            //string _strShi = mutex.GetPulseType(_pulseTypeListTrue);
            //if (_strShi != "" && _pulseTypeListTrue.Contains("实"))
            //{
            //    _pulseTypeListTrue.Remove("实");
            //}

            mutex = new mutexContext("平");
            String _strPing = mutex.GetPulseType(_pulseTypeListTrue);
            if (!TextUtils.isEmpty(_strPing) && _pulseTypeListTrue.contains("平")) {
                _pulseTypeListTrue.remove("平");
            }

            if ("浮取".equals(_plusePresurre)) {
                if (_pulseTypeListTrue.contains("沉")) {
                    _pulseTypeListTrue.remove("沉");
                }
            } else if ("中取".equals(_plusePresurre)) {
                if (_pulseTypeListTrue.contains("沉")) {
                    _pulseTypeListTrue.remove("沉");
                }
                if (_pulseTypeListTrue.contains("浮")) {
                    _pulseTypeListTrue.remove("浮");
                }
            } else {
                if (_pulseTypeListTrue.contains("浮")) {
                    _pulseTypeListTrue.remove("浮");
                }
            }


            for (int i = 0; i < _pulseTypeListTrue.size(); i++) {
                _pulseType += _pulseTypeListTrue.get(i) + ",";
            }

            if (_pulseType.length() > 0) {
                _pulseType = _pulseType.substring(0, _pulseType.length() - 1);
            }
        }

        return _pulseType;

    }
}
